<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="通知名" prop="name">
        <el-input v-model="dataForm.name" placeholder="通知名"></el-input>
      </el-form-item>
      <el-form-item label="通知链接" prop="notify">
        <el-input v-model="dataForm.notify" placeholder="通知链接"></el-input>
      </el-form-item>
      <el-form-item label="发布日期" prop="notifyTime1">
        <div class="block">
          <!-- <span class="demonstration">带快捷选项</span> -->
          <el-date-picker v-model="dataForm.notifyTime1" align="right" type="date" placeholder="选择日期"
            :picker-options="pickerOptions" value-format="yyyy-MM-dd">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="是否开启" prop="statu">
        <!-- <el-input v-model="dataForm.statu" placeholder="是否开启"></el-input> -->
        <el-radio v-model="dataForm.statu" :label="1">是</el-radio>
        <el-radio v-model="dataForm.statu" :label="2">否</el-radio>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        notify: '',
        notifyTime1: '',
        statu: '',
        radio: '1',
        value2: ''
      },
      dataRule: {
        name: [
          { required: true, message: '通知名不能为空', trigger: 'blur' }
        ],
        notify: [
          { required: true, message: '通知链接不能为空', trigger: 'blur' }
        ],
        notifyTime1: [
          { required: true, message: '发布日期不能为空', trigger: 'blur' }
        ],
        statu: [
          { required: true, message: '是否开启不能为空', trigger: 'blur' }
        ]
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      value1: '',

    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/notify/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.notify.name
              this.dataForm.notify = data.notify.notify
              this.dataForm.notifyTime1 = data.notify.notifyTime1
              this.dataForm.statu = data.notify.statu
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/notify/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'notify': this.dataForm.notify,
              'notifyTime1': this.dataForm.notifyTime1,
              'statu': this.dataForm.statu
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
